from scrapy.exceptions import DropItem
from scrapyProject.spiders.proxy.items import Proxyitem
class ItemProxyTxtSave(object):
    def open_spider(self, spider):
        filename = "proxy.txt"
        self.m_file = open(filename, 'w')

    def close_spider(self, spider):
        self.m_file.close()

    def process_item(self, item, spider):
        if isinstance(item, Proxyitem):
            self.m_file.write(item["proxy_scheme"])
            self.m_file.write(" ")
            self.m_file.write(item["proxy"])
            self.m_file.write("\n")
            self.m_file.flush()
        return item

class ItemDuplicates(object):
    def __init__(self):
        self.item_set = set()

    def process_item(self, item, spider):
        if isinstance(item, Proxyitem):
            name = item['proxy']
            if name in self.item_set:
                raise DropItem("Duplicate proxy found: %s" % item)
            self.item_set.add(name)
            return item
        return  item

from scrapyProject.spiders.proxy.proxyManage import proxyManageForMySQL
class ItemProxyDataBaseSave(object):
    def open_spider(self, spider):
        self.manage = proxyManageForMySQL()

    def process_item(self, item, spider):
        sql = ""
        if isinstance(item, Proxyitem):
            self.manage.proxyInsert(item["proxy"], item["proxy_scheme"], item["lastCheckTime"], "None", item["speed"])
        return item